//
//  WYWHTTPSessionManager.m
//  15WClient
//
//  Created by JinYong on 16/4/24.
//  Copyright © 2016年 15w. All rights reserved.
//

#import "WYWHTTPSessionManager.h"
#import "WYWDeviceUtil.h"


//#import "WKLocationManager.h"

//#import "JPUSHService.h"

static const NSTimeInterval kRequestTimeoutInterval = 20.f;

@interface WYWHTTPSessionManager ()
@property (nonatomic, copy) NSString *platform;
@property (nonatomic, copy) NSString *version;
@property (nonatomic, copy) NSString *buildCode;
@property (nonatomic, copy) NSString *os;
@property (nonatomic, copy) NSString *device;
@property (nonatomic, copy) NSString *push;

@property (nonatomic ,copy) NSString *mac;
@property (nonatomic ,copy) NSString *IDFA;

@end

@implementation WYWHTTPSessionManager

+ (instancetype)manager
{
    static dispatch_once_t onceToken;
    __strong static WYWHTTPSessionManager *sharedObject = nil;
    dispatch_once(&onceToken, ^{
        sharedObject = [[self alloc] initWithBaseURL:nil];// or some other init method
        
        sharedObject.responseSerializer = [AFJSONResponseSerializer serializer];
        sharedObject.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json", nil];
        
        // 设置共用的headerField
        // 平台
        NSString *strPlatform = @"iOS";
        sharedObject.platform = strPlatform;;
        
        // app版本
        NSString *app_Version = ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]);
        sharedObject.version = app_Version;
        
        // build号
        NSString *code = ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]);
        NSString *app_build = [NSString stringWithFormat:@"%i",[code intValue]];
        sharedObject.buildCode = app_build;
        
        // 客户端系统
        NSString *strOS = [NSString stringWithFormat:@"iOS%@",[[UIDevice currentDevice] systemVersion]];
        sharedObject.os = strOS?:@"iOS";
        
        // 设备类型
        NSString *strDevice = [WYWDeviceUtil device];
        sharedObject.device = strDevice?:@"iPhone";
        
        // 渠道
        NSUserDefaults *openinstallData = [NSUserDefaults standardUserDefaults];
        NSString *strChannel = [openinstallData objectForKey:@"channel"]?:@"appstore";
        sharedObject.channel = strChannel;
        
        // 分享数据
        sharedObject.params = [openinstallData objectForKey:@"params"]?:@"";
        
        //mac
//        [UIDevice currentDevice].identifierForVendor.UUIDString;
        NSString *strMac = [WYWDeviceUtil macaddress];
        sharedObject.mac = strMac;
        
        //IDFA
        NSString *strIDFA = [WYWDeviceUtil getIDFA];
        sharedObject.IDFA = strIDFA;
        
//        NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
//        [defaultCenter addObserver:sharedObject
//                          selector:@selector(fetchPushId:)
//                              name:kJPFNetworkDidLoginNotification
//                            object:nil];
        
    });
    
    return sharedObject;
}

//- (void)fetchPushId:(NSNotification *)notification {
//    NSString *strPush = [JPUSHService registrationID];
//    self.push = strPush;
//}

- (void)configureHeaderField:(WYWRequestHeaderType)type {
    switch (type) {
        case WYWRequestHeaderTypeHTTP:
            self.requestSerializer = [AFHTTPRequestSerializer serializer];
            break;
        case WYWRequestHeaderTypeJSON:
            self.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case WYWRequestHeaderTypeSpecial1:
            break;
        case WYWRequestHeaderTypeSpecial2:
            break;
    }
    self.requestSerializer.timeoutInterval = kRequestTimeoutInterval;
    
    [self.requestSerializer setValue:self.platform
                          forHTTPHeaderField:@"platform"];
    
    [self.requestSerializer setValue:self.version
                          forHTTPHeaderField:@"version"];
    
    [self.requestSerializer setValue:self.buildCode
                  forHTTPHeaderField:@"versioncode"];
    
    [self.requestSerializer setValue:self.os
                          forHTTPHeaderField:@"os"];
    
    [self.requestSerializer setValue:self.device
                          forHTTPHeaderField:@"device"];
    
    [self.requestSerializer setValue:[WYWDeviceUtil macaddress]
                  forHTTPHeaderField:@"deviceid"];
    
    [self.requestSerializer setValue:self.channel
                          forHTTPHeaderField:@"channel"];
    
    [self.requestSerializer setValue:self.params
                  forHTTPHeaderField:@"params"];
    
    [self.requestSerializer setValue:self.mac
                  forHTTPHeaderField:@"mac"];

    [self.requestSerializer setValue:self.IDFA
                  forHTTPHeaderField:@"IDFA"];

        
//    WKUserModel *userModel = [WKUserInstance sharedInstance].userModel;
//    [self.requestSerializer setValue:userModel.token?:@"" forHTTPHeaderField:@"token"];
//    [self.requestSerializer setValue:userModel.uid?:@"0" forHTTPHeaderField:@"uid"];
    
//    double lat = [WKLocationManager sharedManager].latitude;
//    double lng = [WKLocationManager sharedManager].longitude;
//    [self.requestSerializer setValue:[@(lat) stringValue]
//                  forHTTPHeaderField:@"lat"];
//    [self.requestSerializer setValue:[@(lng) stringValue]
//                  forHTTPHeaderField:@"lng"];
    
    [self.requestSerializer setValue:self.push forHTTPHeaderField:@"push"];
    
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970] * 1000;
    NSString *strTime = [@((long long)currentTime) stringValue];
    
    [self.requestSerializer setValue:strTime
                          forHTTPHeaderField:@"time"];
    
}
@end
